import React from 'react'

/**
 * 按钮根据权限生成
 * @param {*} Component 按钮的类型，Button为普通按钮类型，'a'为行内按钮，还可传入回调，自定义按钮
 * @param {*} permission 按钮的code值，为菜单管理时配置的buttonCode
 */
const withPermission = (Component, permission) => {
  const NewComponent = (props) => {
    const buttonList = localStorage.getItem('buttonList') ? JSON.parse(localStorage.getItem('buttonList')) : [];
    let btn = null;
    buttonList && buttonList.length && buttonList.forEach((item => {
      if (item.buttonCode === permission) {
        btn = <Component {...props} type={item.description}>{props.text ? props.text : item.buttonName}</Component>;
      }
    }));
    return btn;
  }
  return NewComponent;
};

export {
  withPermission,
}
